package mybatis.Test;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.bean.Emp;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.EmpMapper;

public class EmpTest {
	
	private SqlSession sqlSession;
	
	@Before
	public void before()throws Exception {
		InputStream inputSource = Resources.getResourceAsStream("conf/mybatis-config.xml");
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputSource);
		sqlSession = sqlSessionFactory.openSession();
	}
	
	@After
	public void after() {
		sqlSession.commit();
		sqlSession.close();
	}
	
	//查询所有的员工 带分页
	@Test
	public void listEmp() {
		EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
		
		Page<Object> page = PageHelper.startPage(1, 2);
		
		List<Emp> listEmp = mapper.listEmp();
		
		// 第一个参数 查询的数据    第二个参数  连续显示多少页  （当前是  连续显示  3  页 ）
		PageInfo<Emp> pageInfo = new PageInfo<Emp>(listEmp, 3);
		
		for (Emp emp : listEmp) {
			System.out.println(emp.toString());
		}
			
		System.err.println("当前页码："+page.getPageNum());
		System.err.println("总记录数："+page.getTotal());
		System.err.println("每页的记录数："+page.getPageSize());
		System.err.println("总页码："+page.getPages());
		
		System.err.println("连续显示的页码：");
		int[] nums = pageInfo.getNavigatepageNums();
		for (int i : nums) {
			System.err.println(i);
		}
		
	}
	
	
	
	//根据 eid 查询员工
	@Test
	public void getbyidEmp() {
		EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
		Emp getbyidEmp = mapper.getbyidEmp(1);
		System.out.println(getbyidEmp.toString());
	}
	
	//根据 eid 删除员工
	@Test
	public void deleteEmp() {
		EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
		mapper.deleteEmp(15);
	}
	
	//修改员工
	@Test
	public void updateEmp() {
		EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
		Emp emp = new Emp(14, "admin", 120);
		mapper.updateEmp(emp);
	}
	
	
	
	
}







